﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;
using System.IO;
using System.Windows.Forms;

namespace Downloader
{
    class HttpUtil
    {
        internal static long  getHttpFilelength(string url)
        {
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            CookieContainer cookieContainer = new CookieContainer();
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.CookieContainer = cookieContainer;
            //MessageBox.Show(request.Headers.ToString());
            HttpWebResponse response;
            try
            {
                long len = 0;
                response = request.GetResponse() as HttpWebResponse;
                //MessageBox.Show(response.Headers.ToString());
                //MessageBox.Show(response.StatusCode.ToString());
                len = response.ContentLength;
                //sMessageBox.Show(response.GetResponseHeader(""));
                response.Close();
                return len;
            }
            catch (System.Exception ex)
            {
                //MessageBox.Show(ex.Message);
                return -2;
            }
            return -3;
            
        }
        internal static byte[] getHttpFileSlice(string url,long a,long b)
        {
          
            int length = (int)(b-a+1);
            byte []result=null;

            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                CookieContainer cookieContainer = new CookieContainer();
                request.UserAgent = "MSIE 6.0";
                request.CookieContainer = cookieContainer;
                request.AddRange(a, b);
                request.Timeout = 50000;
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                BinaryReader br = new BinaryReader(response.GetResponseStream());
                result = br.ReadBytes(length);
            }catch(Exception e)
            {
                MessageBox.Show(e.Message);
                return null;
            }
            return result;
        }

        internal static bool testRangeAvailable(string url)
        {
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            CookieContainer cookieContainer = new CookieContainer();
            request.UserAgent = "MSIE 6.0";
            request.CookieContainer = cookieContainer;
            request.AddRange(1);
            request.Timeout = 5000;
            HttpWebResponse response = null;
            try
            {
                response = request.GetResponse() as HttpWebResponse;
                if (response.ContentLength == -1)
                    return false;
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
            
            return false;
        }
    }

    class HttpDown
    {
        HttpWebRequest request ;
        CookieContainer cookieContainer;
        HttpWebResponse response;
        BinaryReader br;

        public HttpDown(string url)
        {
            try
            {
                request = WebRequest.Create(url) as HttpWebRequest;
                cookieContainer = new CookieContainer();
                request.UserAgent = "MSIE 6.0";
                request.CookieContainer = cookieContainer;
                request.Timeout = 5000;
                response = request.GetResponse() as HttpWebResponse;
                br = new BinaryReader(response.GetResponseStream());
            }catch(Exception e)
            {
            }
            
        }
        ~HttpDown()
        {
            response.Close();
            br.Close();
        }
        public byte[] ReadBytes(int length,out int readed)
        {
            if (br == null)
            {
                readed = 0;
                return null;
            }
            byte[] buffer = new byte[length];
            readed = br.Read(buffer,0,length);;
            if (readed <= 0)
            {
                return null;
            }
            return buffer;
        }

        
    }
}
